<?php
/*
Author: Nicolas Zdanovicz
Date 20140325
User Data Access Class
*/

class DAODia{

    public static function getById($id){
        
         $db = getPDO();
         $sql = "select id, nombre from dias where id = ?";
         $stmt = $db->prepare($sql);
         $ok = $stmt->execute(array($id));
         $obj = $stmt->fetchObject();
         
         $dia = null;
         if($obj!=null){
             $dia= DAODia::mapValues($obj);
         }
         else{
            throw new ENoDataFound("No se encontro el dia con el id ".$id);
         }
         
         return $dia;
    }
    
    public static function isFeriado($date){
    
    	$nro=intval(date("j",$date));
    	$mes=intval(date("n",$date));
    	$anio=intval(date("Y",$date));
    	
    	$db = getPDO();
    	$sql = "select tipo from feriados where dia = ? and mes = ? and anio = ?";
    	$stmt = $db->prepare($sql);
    	$ok = $stmt->execute(array($nro,$mes,$anio));
    	$obj = $stmt->fetchObject();
    	 
    	$tipo = null;
    	if($obj!=null){
    		$tipo= $obj->tipo;
    	}
    	else{
    		$tipo='N';
    	}
    	 
    	return $tipo;
    }    



    public static function getAll(){
         
         $db = getPDO();
         $sql = "select id, nombre from dias";
         $stmt = $db->prepare($sql);
         $ok = $stmt->execute();
         $result = $stmt->fetchAll(\PDO::FETCH_OBJ);
         $arrResult = array();    
        
         foreach($result as $row){             
             array_push($arrResult,DAODia::mapValues($row));
         }
         
        return $arrResult;
    }
    
    
    

    private static function mapValues($obj){
        $dia = new Dia();
		
		$dia->setId($obj->id);
		$dia->setNombre($obj->nombre);
        return $dia;
    }
    
}
?>